package com.higgins.client.knowledge.trainer;

import java.util.ArrayList;
import java.util.List;

import com.higgins.client.knowledge.KnowledgeItemDTO;

public class KnowledgeTrainerOracle {
	List<KnowledgeItemDTO> knowledgeItems;
	List<KnowledgeItemDTO> remainingQuestions;
	KnowledgeItemDTO answerItem = null;
	Integer answerScore = null;
	String answer = null;
	List<String> answerWords = null;
	int accuracy = 3;
	int minWordLength = 3;
	
	public KnowledgeTrainerOracle()	{
		
	}
	public void setKnowledgeItems(List<KnowledgeItemDTO> knowledgeItems)	{
		this.knowledgeItems = knowledgeItems;
		remainingQuestions = new ArrayList<KnowledgeItemDTO>();
		for (KnowledgeItemDTO knowledgeItem:knowledgeItems)	{
			remainingQuestions.add(knowledgeItem);
		}
	}
	public void setAnswer(String answer)	{
		this.answer = answer;
		this.answerItem = null;
		this.answerScore = null;
	}
	public boolean identifyCorrectAnswer()	{
		processSentence();
		answerScore = 0;
		for (KnowledgeItemDTO dto:remainingQuestions)	{
			List<String> comparisonWords = processSentence(dto.getItem());
			int score = compareWords(answerWords, comparisonWords);
			if (score > answerScore)	{
				answerScore = score;
				answerItem = dto;
			}
		}
		return answerItem!=null;
	}
	public Integer retrieveScore()	{
		return answerScore;
	}
	public String retrieveCorrectAnswer()	{
		if (answerItem!=null)	{
			return answerItem.getItem();
		} 
		return null;
	}
	public Long retrieveAnswerId()	{
		if (answerItem!=null)	{
			return answerItem.getId();
		} 
		return null;
	}
	public boolean nextQuestion()	{
		if (answerItem!=null)	{
			return remainingQuestions.remove(answerItem);
		}
		return false;
	}	
	
	private int compareWords(List<String> answerWords, List<String> comparisonWords)	{
		int numberOfRelevantWords = 0;
		int correctRelevantWords = 0;
		List<String> trimmedComparisonWords = trimWords(comparisonWords);
		for (String word:answerWords)	{
			if (word.length() >= minWordLength)	{
				numberOfRelevantWords++;
				String trimmedWord = trimWord(word);
				if (trimmedComparisonWords.contains(trimmedWord))	{
					correctRelevantWords++;
				}
			}
		}
		return 100*correctRelevantWords/numberOfRelevantWords;
	}
	private String trimWord(String word)	{
		String trimmedWord = word.length() > accuracy ? word.substring(0, accuracy):word;
		return trimmedWord;
	}
	private List<String> trimWords(List<String> words)	{
		List<String> trimmedWords = new ArrayList<String>();
		for (String word:words)	{
			trimmedWords.add(trimWord(word));
		}
		return trimmedWords;
	}
	private List<String> processSentence(String sentence)	{
		String tempSentence = new String(sentence);
		List<String> words = new ArrayList<String>();
		while (tempSentence.contains(" "))	{
			int beginning = 0;
			int end = tempSentence.indexOf(" ");
			String word = tempSentence.substring(beginning, end);
			words.add(word);
			tempSentence = tempSentence.substring(end + 1);
		}
		words.add(tempSentence);
		return words;
	}
	private void processSentence()	{
		answerWords = processSentence(answer);
	}
	public List<KnowledgeItemDTO> getRemainingQuestions()	{
		return remainingQuestions;
	}
}
